// thread.php请求
// noinspection JSUnusedLocalSymbols

import {MyCache} from "@ginstone/common-utils/dist/src/utils/MyCache";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";

//判断一个主题是否应当被屏蔽
const blockMatch = (topic, {keywords, users}) => {
    for (let i = 0; i < keywords.length; i++) {
        if (topic.hasOwnProperty("title") && topic.title.includes(keywords[i])) {
            return false;
        }
    }

    for (let i = 0; i < users.length; i++) {
        const [uid, username] = users[i].split('/')
        if (topic.hasOwnProperty("authorName") && username === topic.authorName) {
            return false;
        }
        if (topic.hasOwnProperty("authorUid") && Number(uid) === topic.authorUid) {
            return false;
        }
    }
    return true;
}

export default {
    namespaced: true,
    state: {
        cache: new MyCache(),
        privilegeCache: new MyCache(),
    },
    mutations: {
        method(state, payload) {

        },
    },
    actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {

        },
        privilegeList: ({dispatch, commit, state, rootGetters}, {fid, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.privilegeList(client, fid)
            return state.privilegeCache.use({fid}, method, force)
        },
        thread: ({dispatch, commit, state, rootGetters}, {params, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => client.thread(params).then(async res => {
                if (!params.hasOwnProperty('authorid')) {
                    const blockData = await dispatch("Block/getBlockData", false, {root: true})
                    res.blockedTopics = res.topics.filter(i => !blockMatch(i, blockData))
                    res.topics = res.topics.filter(i => blockMatch(i, blockData))
                }
                return res;
            })
            return state.cache.use(params, method, force)
        },
    },
    getters: {},
}